Basic Workflows in Git


Initializing a Repo

To start a git repository, you must first initialize the directory in which the project is located in. To do this you will need to use the git init command on the terminal (Mac/Linux) or command prompt (Windows). This command creates a .git folder that contains all the tools and data necessary to contain various versions of our project.

Adding Changes

To add a file's changes to the staging area, you will need to use the git add command, and use it like the following:

$ git add <file>

If you want to add all the files, then use the wildcard, git add *.

Now let's say there's files or directory you don't want added to the git repository (possibly build directories or large files), you can create a .gitignore file, and in it add the files or directories you want ignored.

# .gitignore example 
example_directory/
example.txt
*.zip # this will ignore all zip files 

Committing Changes & Status

So let's say you're satisfied with the changes you've made to the various files you've worked on. You could first see which file has been changed, and/or changes been added, once you've added the files that's had their changes made, what now? Well when you add these files, they're in a staging area, to commit these changes to the repository, well it's exactly what you think, use git commit.

git commit -m "A nice message about the changes" # Standard commit command 
git commit -a -m "A nice message" # Adds and commits 

Differences & Logs

Let's say you want to figure out the different changes between a file from the directory and the staging area version of the file, well as straightforward the commands are, it is the git diff command, and all you need to do is git diff <file>.

Now let's say you want to view the various commits that you've done in the git repository, use the git log command, and it will show you the commits you've done with the following information:

  • A 40-character code (SHA), that creates a unique id for the commit
  • The commit author
  • The date and time of which the commit occured
  • The message written on the commit

Here's an example of a git log done on this repo:

commit e5af29e89846a396690e9d73d771ef6b298ca1a0 (HEAD -> main)
Author: mustafif0929@gmail.com <mustafif0929@gmail.com>
Date:   Thu Apr 22 22:37:33 2021 -0400

    update

commit 3fc278bcf2e74d151ead9192ef1991a235044c71 (origin/main)
Author: mustafif0929@gmail.com <mustafif0929@gmail.com>
Date:   Thu Apr 22 22:21:18 2021 -0400

    first commit

Git Project Workflow

A Git Project contains three parts:

  1. Working Directory: where files are created, edited, deleted and organized (worked on).
  2. Staging Area: where changes that are made to the working directory is listed
  3. Repository: where Git permanently stores changes as different versions of the project

The Git workflow consists of editing files in the working directory, adding files to the staging area, and saving changes to a Git repository.